<%@ page import="com.wl.shixun2.dao.ProductDAO" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("UTF-8");
%>
<html>
<head>
    <title>新增产品 - 2350309097 万朗</title>
    <link rel="stylesheet" href="css/addProduct.css" type="text/css">
    <script>
        // 页面加载完成后，如果有消息则2秒后淡出
        document.addEventListener('DOMContentLoaded', function() {
            var messageElements = document.querySelectorAll('.success, .error');
            if (messageElements.length > 0) {
                setTimeout(function() {
                    messageElements.forEach(function(element) {
                        element.classList.add('fade-out');
                    });
                }, 2000); // 2秒后开始淡出
            }
        });
    </script>
</head>
<body>
<h2>添加新产品</h2>

<%
    String message = "";
    String errorMessage = "";
    
    // 处理表单提交
    if (request.getMethod().equals("POST")) {
        try {
            String number = request.getParameter("number");
            String name = request.getParameter("name");
            String priceStr = request.getParameter("price");
            String madetimeStr = request.getParameter("madetime");
            
            if (ProductDAO.insertFromParams(number, name, priceStr, madetimeStr)) {
                message = "产品添加成功";
            } else {
                errorMessage = "添加产品失败";
            }
        } catch (IllegalArgumentException e) {
            errorMessage = e.getMessage();
        } catch (Exception e) {
            errorMessage = "添加产品时出错: " + e.getMessage();
        }
    }
%>



<form method="post" action="">
    <div class="form-group">
        <label for="number">产品号:</label>
        <input type="text" id="number" name="number" required>
    </div>
    
    <div class="form-group">
        <label for="name">名称:</label>
        <input type="text" id="name" name="name" required>
    </div>
    
    <div class="form-group">
        <label for="price">价格:</label>
        <input type="number" id="price" name="price" required>
    </div>
    
    <div class="form-group">
        <label for="madetime">生产日期:</label>
        <input type="datetime-local" id="madetime" name="madetime" required>
    </div>
    
    <div class="form-group">
        <input type="submit" value="添加产品">
        <input type="button" class="cancel-btn" value="取消" onclick="window.location.href='productList.jsp'">
    </div>
</form>
<% if (!message.isEmpty()) { %>
<div class="success"><%= message %></div>
<% } %>

<% if (!errorMessage.isEmpty()) { %>
<div class="error"><%= errorMessage %></div>
<% } %>
</body>
</html>